package com.ce.pms.renovation.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.common.dao.IOrganizationalStructureDao;
import com.ce.pms.common.model.HmEmployeeDomain;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.renovation.dao.IApprovalItemDao;
import com.ce.pms.renovation.dao.IDepositAccountApprovalDao;
import com.ce.pms.renovation.dao.IDepositAccountingDao;
import com.ce.pms.renovation.dao.IRenovateApprovalDao;
import com.ce.pms.renovation.dao.IRenovateRequisitionDao;
import com.ce.pms.renovation.model.ApprovalItemDO;
import com.ce.pms.renovation.model.DepositAccountingApprovalDO;
import com.ce.pms.renovation.model.DepositAccountingDO;
import com.ce.pms.renovation.model.RenovateApprovalDO;
import com.ce.pms.renovation.model.RenovateRequisitionDO;
import com.ce.pms.renovation.service.IFlowManageService;
import com.ce.pms.renovation.service.IRenovateChargeService;
import com.ce.pms.renovation.vo.ApprovalItemVO;
import com.ce.pms.renovation.vo.DepositAccountingApprovalVO;
import com.ce.pms.renovation.vo.RenovateApprovalVO;
import com.ce.pms.system.renovation.dao.IApprovalItemConfigDao;
import com.ce.pms.system.renovation.model.ApprovalItemConfigDO;
import com.ce.util.BeanUtil;
import com.ce.util.BlankUtil;

/**
 * 流程管理服务类
 */
public class FlowManageServiceImpl extends PmsBaseServiceImpl implements IFlowManageService {
	private static final String NO_PASSED = "不通过";

	private static final String PASSED = "通过";

	private IApprovalItemConfigDao approvalItemConfigDao;

	private IApprovalItemDao approvalItemDao;

	private IDepositAccountApprovalDao depositAccountApprovalDao;

	private IDepositAccountingDao depositAccountingDao;

	private IOrganizationalStructureDao organizationalStructureDao;

	private IRenovateApprovalDao renovateApprovalDao;

	private IRenovateChargeService renovateChargeService;

	private IRenovateRequisitionDao renovateRequisitionDao;

	/**
	 * 功能描述:对保证金核算结果进行审批<br>
	 * 必填项：核算记录ID和审批人ID
	 * @param depositAccountingAppVO
	 * Create author:罗军林<br>
	 * Create on:2009-1-2<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void approveDepositAccounting(DepositAccountingApprovalVO depositAccountingAppVO) {
		isAllowApproveDepositAccounting(depositAccountingAppVO.getApprovalId(),depositAccountingAppVO.getDepositAccountingId());

		DepositAccountingApprovalDO depositAccountingApp =
			getDepositAccountingApprovalDo(depositAccountingAppVO.getApprovalId(),depositAccountingAppVO.getDepositAccountingId());

		depositAccountingApp.setApprovalTime(this.getDate());
		depositAccountingApp.setHandleYn(SysrefcodeDO.CONSTANTS_DIGITAL_ONE);
		depositAccountingApp.setRemark(depositAccountingAppVO.getRemark());
		depositAccountingApp.setResult(depositAccountingAppVO.getResult());

		if (SysrefcodeDO.RM_APPROVAL_RESULT_PASS.equals(depositAccountingAppVO.getResult())) {
			depositAccountingApp.setCurrentNodeYn(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO);
			depositAccountingApp.getDepositAccounting().setApprovalState(SysrefcodeDO.RM_DEPOSIT_ACCOUNTING_APPROVAL_STATE_PASS);
		}else{
			depositAccountingApp.getDepositAccounting().setApprovalState(SysrefcodeDO.RM_DEPOSIT_ACCOUNTING_APPROVAL_STATE_NO);
		}
		notifyApproveDepositAccounting(depositAccountingApp.getKeyword(),depositAccountingAppVO.getCreator());
	}

	/**
	 * 功能描述:一级审批<br>
	 * @param approvalVO
	 * <br>
	 * Create author:罗军林<br>
	 * Create on:2008-12-31<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void firstRenovateApproval(RenovateApprovalVO approvalVO) {
		isAllowSubmitRenovateApproval(approvalVO.getRequistionId(), approvalVO.getApprovalId());
		changeApprovalResult(approvalVO);

		notifyFirstRenovateApproval(approvalVO.getRequistionId());
	}

	/**
	 * 功能描述:查询指定装修申请的所有已经审核过的装修申请审批结果<br>
	 *
	 * @param requisitionId
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-12-31<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public List<RenovateApprovalVO> getAllIsHandleRenovateApproval(Long requisitionId) {
		List<RenovateApprovalDO> renovateApprovalList = renovateApprovalDao
				.getAllIsHandleRenovateApproval(requisitionId);
		List<RenovateApprovalVO> temp = new ArrayList<RenovateApprovalVO>();
		for (RenovateApprovalDO item : renovateApprovalList) {
			temp.add(assembleRenovateApprovalV2D(item));
		}
		return temp;
	}

	/**
	 * 功能描述:取得指定核算记录的审批结果<br>
	 * @param depositAccountingId
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2009-1-7<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public DepositAccountingApprovalVO getApproveDepositAccountingBy(Long depositAccountingId){
		DepositAccountingApprovalDO appro= depositAccountApprovalDao.getDepositAccountingApproval(depositAccountingId);

		return appro==null ? null : assembleDepositAccountingApprovalD2V(appro);
	}

	/**
	 * 功能描述:查询指定审批人、指定保证金核算结果ID审批记录<br>
	 *
	 * @param approvalId
	 * @param depositAccountId
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2009-1-6<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public DepositAccountingApprovalVO getDepositAccountingApproval(Long approvalId, Long depositAccountId) {

		DepositAccountingApprovalDO approval = depositAccountApprovalDao.getDepositAccountingApproval(approvalId,
				depositAccountId);
		if (BlankUtil.isBlank(approval)){
			return null;
		}
//		checkParameterIsNull(approval, "指定审批人没有该记录的审批权限");
		return assembleDepositAccountingApprovalD2V(approval);
	}

	/**
	 * @param renovateRequisitionId
	 * @return List
	 * @roseuid 49473AFA02EF
	 */
	public List getFirstRenovateApprovals(Long renovateRequisitionId) {
		return null;
	}

	/**
	 * 功能描述:得到指定审批人的装修申请审批<br>
	 *
	 * @param employeeId
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-12-31<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public RenovateApprovalVO getRenovateApproval(Long employeeId, Long requisitionId) {
		RenovateApprovalDO renovateApprovalDO = renovateApprovalDao.getRenovateApproval(employeeId, requisitionId);
		return assembleRenovateApprovalV2D(renovateApprovalDO);
	}

	/**
	 * 功能描述:是否允许对核算进行审批<br>
	 * 需要的字段有：审批人ID,核算记录ID，如果没有或者不满足条件将抛出异常
	 * @param depositAccountingAppVO
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2009-1-7<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public boolean isAllowApproveDepositAccounting(Long approvalId,Long depositAccountingId) {
		checkParameterIsNull(depositAccountingId, "renovate:renovate_deposit_not_count");
		checkParameterIsNull(approvalId,"renovate:renovate_must_has_approval_main");

		DepositAccountingApprovalDO depositAccountingApp = getDepositAccountingApprovalDo(approvalId,depositAccountingId);

		checkParameterIsNull(depositAccountingApp, "renovate:renovate_has_no_pright");
		if( !SysrefcodeDO.RM_DEPOSIT_ACCOUNTING_APPROVAL_STATE_WAIT.equals(
				depositAccountingApp.getDepositAccounting().getApprovalState()) ){
			throw new BusinessException("renovate:renovate_must_wait_approval");
		}
		return true;
	}

	/**
	 * 功能描述:是否允许审批<br>
	 *
	 * @param requistionId
	 * @param abstractApprovalId
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2009-1-5<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public Boolean isAllowSubmitRenovateApproval(Long requistionId, Long abstractApprovalId) {
		RenovateApprovalDO renovateApproval = renovateApprovalDao.getRenovateApproval(abstractApprovalId, requistionId);
		RenovateRequisitionDO requisitionDO = renovateRequisitionDao.findById(requistionId);
		if( !(SysrefcodeDO.RM_RENOVATE_REQUISITION_STATE_FARE.equals( requisitionDO.getRequisitionState())
				|| SysrefcodeDO.RM_RENOVATE_REQUISITION_STATE_WAIT.equals( requisitionDO.getRequisitionState())) ){
			throw new BusinessException("renovate:renovate_must_wait_fare_approval",BusinessException.ERROR);
		}
		if (null == renovateApproval) {
			throw new BusinessException("renovate:renovate_has_no_pright");
		}  else if( SysrefcodeDO.RM_APPROVAL_LEVEL_TWO.equals( renovateApproval.getLevel())
				&& SysrefcodeDO.CONSTANTS_DIGITAL_ZERO.equals(renovateApproval.getCurrentNodeYn()) ){
			throw new BusinessException("renovate:renovate_first_not_pass");
		} else if( SysrefcodeDO.RM_APPROVAL_LEVEL_ONE.equals( renovateApproval.getLevel())
				&& SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(renovateApproval.getHandleYn())
				&& SysrefcodeDO.RM_RENOVATE_REQUISITION_STATE_WAIT.equals(renovateApproval.getRenovateRequistion().getRequisitionState())){
			throw new BusinessException("renovate:renovate_must_wait_approval");
		} else if( SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(renovateApproval.getHandleYn()) ){
			throw new BusinessException("renovate:renovate_has_approvaled",BusinessException.ERROR);
		}
		return true;
	}

	/**
	 * 功能描述:二级审批<br>
	 * @param approvalVO<br>
	 * Create author:罗军林<br>
	 * Create on:2009-1-15<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void secondRenovateApproval(RenovateApprovalVO approvalVO) {
		isAllowSubmitRenovateApproval(approvalVO.getRequistionId(), approvalVO.getApprovalId());

		changeApprovalResult(approvalVO);

		notifySecondRenovateApproval(approvalVO.getRequistionId());
	}





	/**
	 * 功能描述:将退还保证金核算结果提交给指定员工进入审核状态<br>
	 * @param depositAccountingId
	 * @param employeeId
	 * @param creator<br>
	 * Create author:罗军林<br>
	 * Create on:2009-1-7<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void submitDepositAccounting(Long depositAccountingId, Long employeeId, Long creator) {
		checkParameterIsNull(depositAccountingId, "renovate:renovate_has_not_count_not_submit");
		DepositAccountingDO depositAccounting= depositAccountingDao.findById(depositAccountingId);
		if( !SysrefcodeDO.RM_DEPOSIT_ACCOUNTING_APPROVAL_STATE_NEW.equals(depositAccounting.getApprovalState()) ){
			throw new BusinessException("renovate:renovate_new_submit",BusinessException.ERROR);
		}
		addDepositAccountingApproval(depositAccountingId, employeeId, creator);
	}

	/**
	 * 功能描述:提交装修申请审批<br>
	 *
	 * @param requisitionId
	 * @param firstApprovalIds
	 * @param secondApprovalId
	 * Create author:罗军林<br>
	 * Create on:2008-12-31<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void submitRenovateRequisition(Long requisitionId, Set<Long> firstApprovalIds, Long secondApprovalId,
			Long creator) {
		isAllowSubmitRenovateRequisition(requisitionId);

		RenovateRequisitionDO renovateRequisitionDO = renovateRequisitionDao.findById(requisitionId);
		for (Long employeeId : firstApprovalIds) {
			addRenovateApprovalDO(creator, employeeId, renovateRequisitionDO, SysrefcodeDO.RM_APPROVAL_LEVEL_ONE);
		}
		addRenovateApprovalDO(creator, secondApprovalId, renovateRequisitionDO, SysrefcodeDO.RM_APPROVAL_LEVEL_TWO);

		notifySubmitRenovateRequisition(requisitionId);
	}

	/**
	 * 功能描述:构建预设的指定审批人的审批项目，并返回<br>
	 *
	 * @param employeeId
	 * @param renovateApprovalDO
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-12-31<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private Set<ApprovalItemDO> addApprovalItemDOs(Long employeeId, RenovateApprovalDO renovateApprovalDO) {
		List<ApprovalItemConfigDO> itemConfigList = approvalItemConfigDao.getItemConfigs(employeeId);
		Set<ApprovalItemDO> approvalItems = new HashSet<ApprovalItemDO>();
		for (ApprovalItemConfigDO itemConfig : itemConfigList) {
			ApprovalItemDO approvalItem = new ApprovalItemDO();
			approvalItem.setApproval(renovateApprovalDO);
			approvalItem.setItemConfig(itemConfig);
			approvalItem.setResult(SysrefcodeDO.RM_APPROVAL_ITEM_RESULT_NO);
			approvalItemDao.saveObject(approvalItem);
			approvalItems.add(approvalItem);
		}
		return approvalItems;
	}

	/**
	 * 功能描述:增加一个保证金的审批<br>
	 * @param depositAccountingId
	 * @param employeeId
	 * @param creator<br>
	 * Create author:罗军林<br>
	 * Create on:2009-1-7<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void addDepositAccountingApproval(Long depositAccountingId, Long employeeId, Long creator) {
		DepositAccountingApprovalDO depositAccountingApp = new DepositAccountingApprovalDO();
		depositAccountingApp.setApprovalMan(organizationalStructureDao.findById(employeeId));
		depositAccountingApp.setCurrentNodeYn(SysrefcodeDO.CONSTANTS_DIGITAL_ONE);
		depositAccountingApp.setDepositAccounting(depositAccountingDao.findById(depositAccountingId));
		depositAccountingApp.setHandleYn(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO);
		depositAccountingApp.setSysInfo(this.createInfo(creator));
		depositAccountingApp.getDepositAccounting().setApprovalState(SysrefcodeDO.RM_DEPOSIT_ACCOUNTING_APPROVAL_STATE_WAIT);
		depositAccountApprovalDao.saveObject(depositAccountingApp);
	}

	/**
	 * 功能描述:新增一个装修申请审批，并持久化<br>
	 * 如果一级审批人没有任何的审批项目，将抛出异常<br>
	 *
	 * @param creator
	 * @param renovateRequisitionDO
	 * @param employeeId
	 * <br>
	 * Create author:罗军林<br>
	 * Create on:2008-12-31<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void addRenovateApprovalDO(Long creator, Long employeeId, RenovateRequisitionDO renovateRequisitionDO,
			String level) {
		HmEmployeeDomain employee = organizationalStructureDao.findById(employeeId);
		RenovateApprovalDO renovateApprovalDO = new RenovateApprovalDO();
		renovateApprovalDO.setApprovalMan(employee);
		renovateApprovalDO.setHandleYn(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO);
		renovateApprovalDO.setLevel(level);
		renovateApprovalDO.setRenovateRequistion(renovateRequisitionDO);
		renovateApprovalDO.setSysInfo(this.createInfo(creator));
		if (level.equals(SysrefcodeDO.RM_APPROVAL_LEVEL_ONE)) {
			renovateApprovalDO.setCurrentNodeYn(SysrefcodeDO.CONSTANTS_DIGITAL_ONE);
		} else {
			renovateApprovalDO.setCurrentNodeYn(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO);
		}
		renovateApprovalDao.saveObject(renovateApprovalDO);
		Set<ApprovalItemDO> approvalItems = addApprovalItemDOs(employee.getEmployeeId(), renovateApprovalDO);
		renovateApprovalDO.setApprovalItems(approvalItems);
		if (level.equals(SysrefcodeDO.RM_APPROVAL_LEVEL_ONE) && approvalItems.size() == 0) {
			throw new BusinessException("renovate:renovate_has_not_approval_item,"+employee.getEmployeeName() , BusinessException.ERROR);
		}
		// renovateApprovalDao.saveObject(renovateApprovalDO);
	}



	/**
	 * 功能描述:<br>
	 *
	 * @param approval
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2009-1-6<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private DepositAccountingApprovalVO assembleDepositAccountingApprovalD2V(DepositAccountingApprovalDO approval) {
		DepositAccountingApprovalVO approvalVo = new DepositAccountingApprovalVO();
		BeanUtil.convertVoAsDo(approval, approvalVo);
		return approvalVo;
	}


	/**
	 * 功能描述:装修审请审批的装配<br>
	 *
	 * @param renovateApprovalDO
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-12-31<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private RenovateApprovalVO assembleRenovateApprovalV2D(RenovateApprovalDO renovateApprovalDO) {
		RenovateApprovalVO renovateApprovalVO = new RenovateApprovalVO();
		BeanUtil.convertVoAsDo(renovateApprovalDO, renovateApprovalVO);
		for (ApprovalItemDO appItem : renovateApprovalDO.getApprovalItems()) {
			ApprovalItemVO appItemVO = new ApprovalItemVO();
			appItemVO.setItemName(appItem.getItemConfig().getItemName());
			appItemVO.setKeyword(appItem.getKeyword());
			appItemVO.setResult(appItem.getResult());

			renovateApprovalVO.addApprovalItems(appItemVO);
		}

		if (null != renovateApprovalDO.getResult()
				&& SysrefcodeDO.RM_APPROVAL_RESULT_PASS.equals(renovateApprovalDO.getResult())) {
			renovateApprovalVO.setResultView(PASSED);
		} else if (null != renovateApprovalDO.getResult()
				&& SysrefcodeDO.RM_APPROVAL_RESULT_NO.equals(renovateApprovalDO.getResult())) {
			renovateApprovalVO.setResultView(NO_PASSED);
		}
		renovateApprovalVO.setApprovalMan(renovateApprovalDO.getApprovalMan().getEmployeeName());
		renovateApprovalVO.setApprovalLevel(renovateApprovalDO.getLevel());
		renovateApprovalVO.setRequistionId(renovateApprovalDO.getRenovateRequistion().getKeyword());
		return renovateApprovalVO;
	}

	/**
	 * 功能描述:修改审批结果<br>
	 *
	 * @param approvalVO
	 * Create author:罗军林<br>
	 * Create on:2009-1-2<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void changeApprovalResult(RenovateApprovalVO approvalVO) {
		RenovateApprovalDO renovateApprovalDO = renovateApprovalDao.findById(approvalVO.getKeyword());
		renovateApprovalDO.setResult(approvalVO.getResult());
		renovateApprovalDO.setRemark(approvalVO.getRemark());
		renovateApprovalDO.setApprovalTime(this.getDate());
		renovateApprovalDO.setHandleYn(SysrefcodeDO.CONSTANTS_DIGITAL_ONE);
		// renovateApprovalDO.setApprovalMan(
		// organizationalStructureDao.findById( approvalVO.getApprovalId()) );
		// renovateApprovalDO.setLevel(SysrefcodeDO.RM_APPROVAL_LEVEL_ONE);
		// renovateApprovalDO.setRenovateRequistion(
		// renovateRequisitionDao.findById(approvalVO.getRequistionId()));

		this.modifyInfo(renovateApprovalDO.getSysInfo(), approvalVO.getCreator());
		for (ApprovalItemDO item : renovateApprovalDO.getApprovalItems()) {
			ApprovalItemVO approvalItemVO = approvalVO.getApprovalItems().get(item.getKeyword());
			if( approvalItemVO==null ) continue;
			item.setResult(approvalItemVO.getResult());
		}
	}

	/**
	 * 功能描述:审批完成后的后期处理<br>
	 * 1）如果一级审批全部审批通过，将装修申请状态置为通过
	 * 2) 如果一级审批全部审批完成，且一个或以上的人不通过，则新建一条，老的将异常完结
	 * @param requistionDo
	 * @param approvalList<br>
	 * @param level
	 * Create author:罗军林<br>
	 * Create on:2009-1-9<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void checkRenovateApproval(RenovateRequisitionDO requistionDo, List<RenovateApprovalDO> approvalList,String level) {
		if (isApprovalPass(approvalList, level) && level.equals(SysrefcodeDO.RM_APPROVAL_LEVEL_TWO)) {
			requistionDo.setRequisitionState(SysrefcodeDO.RM_RENOVATE_REQUISITION_STATE_PASS);
		} else
			if( isFinishedApprovalPass(approvalList, level)){
			requistionDo.setRequisitionState(SysrefcodeDO.RM_RENOVATE_REQUISITION_STATE_NPASS);
			RenovateRequisitionDO renovateRequisitionNew=new RenovateRequisitionDO();
			BeanUtil.convertVoAsDo(requistionDo, renovateRequisitionNew);
			renovateRequisitionNew.setKeyword(null);
			renovateRequisitionNew.setRenovateChargeItemDOSet(requistionDo.getRenovateChargeItemDOSet());
			renovateRequisitionNew.setHouse(requistionDo.getHouse());
			renovateRequisitionNew.setSysInfo(requistionDo.getSysInfo());
			renovateRequisitionNew.setRequisitionState(SysrefcodeDO.RM_RENOVATE_REQUISITION_STATE_NEW);
			requistionDo.setRenovateChargeItemDOSet(null);
			requistionDo.setProcessState(SysrefcodeDO.RM_RENOVATE_PROCESS_STATE_EXP);
			renovateRequisitionDao.saveObject(renovateRequisitionNew);

			renovateChargeService.moveRenovateChargeItemToNewQuistion(requistionDo.getKeyword(), renovateRequisitionNew.getKeyword());
		}else {
			requistionDo.setRequisitionState(SysrefcodeDO.RM_RENOVATE_REQUISITION_STATE_FARE);
		}
	}

	/**
	 * 功能描述:<br>
	 * @param depositAccountingAppVO
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2009-1-7<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private DepositAccountingApprovalDO getDepositAccountingApprovalDo(Long approvalId,Long depositAccountingId) {
		return depositAccountApprovalDao
				.getDepositAccountingApproval(approvalId, depositAccountingId);
	}


	/**
	 * 功能描述:是否允许提交<br>
	 *
	 * @param renovateRequisitionId
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2009-1-4<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private Boolean isAllowSubmitRenovateRequisition(Long renovateRequisitionId) {
		RenovateRequisitionDO requistion = renovateRequisitionDao.findById(renovateRequisitionId);
		if (!SysrefcodeDO.RM_RENOVATE_REQUISITION_STATE_NEW.equals(requistion.getRequisitionState())) {
			throw new BusinessException("renovate:renovate_new_submit",BusinessException.ERROR);
		}
		return true;
	}

	/**
	 * 功能描述:指定的审批级别是否全部审批通过，如果全部通过返回true,否则返回false<br>
	 *
	 * @param approvalList
	 * <br>
	 * Create author:罗军林<br>
	 * Create on:2008-12-31<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private Boolean isApprovalPass(List<RenovateApprovalDO> approvalList, String level) {
		for (RenovateApprovalDO item : approvalList) {
			if(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO.equals(item.getHandleYn()) && level.equals(item.getLevel())){
				//当前审批记录未审批过，返回false
				return false;
			}else if( item.getResult()==null && level.equals(item.getLevel()) ){
				//当前审批记录结果为null,返回false
				return false;
			}else if( SysrefcodeDO.RM_APPROVAL_ITEM_RESULT_NO.equals(item.getResult()) && level.equals(item.getLevel()) ){
				// 当前审批记录结果为不通过,返回false
				return false;
			}
		}
		return true;
	}

	/**
	 * 功能描述:指定的审批级别是否全部审批完成，如果全部审批完成且只要有一个不通过则返回true,否则返回false<br>
	 * @param approvalList
	 * @param level
	 * <br>
	 * Create author:罗军林<br>
	 * Create on:2008-12-31<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private Boolean isFinishedApprovalPass(List<RenovateApprovalDO> approvalList, String level) {
		//检测是否都已经审批过，如果有一项没有审批，将返回false
		for (RenovateApprovalDO item : approvalList) {
			if (SysrefcodeDO.CONSTANTS_DIGITAL_ZERO.equals(item.getHandleYn()) && level.equals(item.getLevel())){
				return false;
			}
		}
		//如果有一个审批不通过，将返回true
		for (RenovateApprovalDO item : approvalList) {
			if ( level.equals(item.getLevel())	&& SysrefcodeDO.RM_APPROVAL_ITEM_RESULT_NO.equals(item.getResult())) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 通知完成保证金核算结果审批
	 *
	 * @param depositAccountApprovalId -
	 * 审批结果ID
	 * @roseuid 4940B1590232
	 */
	private void notifyApproveDepositAccounting(Long depositAccountApprovalId,Long creator) {
		DepositAccountingApprovalDO depositAccountingApp = depositAccountApprovalDao.findById(depositAccountApprovalId);
		if (SysrefcodeDO.RM_APPROVAL_ITEM_RESULT_PASS.equals(depositAccountingApp.getResult())) {
			DepositAccountingDO depositAccounting = depositAccountingApp.getDepositAccounting();
			depositAccounting.setApprovalState(
					SysrefcodeDO.RM_DEPOSIT_ACCOUNTING_APPROVAL_STATE_PASS);
			renovateChargeService.notifyAddApproveDepositAccountingChargerecord(depositAccounting.getKeyword(), creator);
		} else {
			depositAccountingApp.getDepositAccounting().setApprovalState(
					SysrefcodeDO.RM_DEPOSIT_ACCOUNTING_APPROVAL_STATE_NO);
			depositAccountingApp.getDepositAccounting().setApprovalState(SysrefcodeDO.RM_RENOVATE_REQUISITION_STATE_NEW);
			depositAccountApprovalDao.deleteObject(depositAccountingApp);
		}

	}

	/**
	 * 功能描述:通知已做一级审批<br>
	 * 规则：如果一级审批人全部通过，将二级审批置为当前节点<br>
	 *
	 * @param approvalList
	 * <br>
	 * Create author:罗军林<br>
	 * Create on:2008-12-31<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void notifyFirstRenovateApproval(Long renovateRequisitionId) {
		RenovateRequisitionDO requistionDo = renovateRequisitionDao.findById(renovateRequisitionId);
		List<RenovateApprovalDO> approvalList = renovateApprovalDao.getAllRenovateApproval(renovateRequisitionId);
		setApprovalNote(approvalList, SysrefcodeDO.RM_APPROVAL_LEVEL_ONE, SysrefcodeDO.RM_APPROVAL_LEVEL_TWO);
		requistionDo.setRequisitionState(SysrefcodeDO.RM_RENOVATE_REQUISITION_STATE_FARE);

		checkRenovateApproval(requistionDo, approvalList, SysrefcodeDO.RM_APPROVAL_LEVEL_ONE);
	}

	/**
	 * 功能描述:通知已做二级审批<br>
	 * 如果审批不通过，则复制一条新的装修申请
	 * @param renovateRequisitionId
	 * Create author:罗军林<br>
	 * Create on:2009-1-2<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void notifySecondRenovateApproval(Long renovateRequisitionId) {
		RenovateRequisitionDO requistionDo = renovateRequisitionDao.findById(renovateRequisitionId);
		List<RenovateApprovalDO> approvalList = renovateApprovalDao.getAllRenovateApproval(renovateRequisitionId);
		setApprovalNote(approvalList, SysrefcodeDO.RM_APPROVAL_LEVEL_TWO, null);

		checkRenovateApproval(requistionDo, approvalList,SysrefcodeDO.RM_APPROVAL_LEVEL_TWO);
	}

	/**
	 * 功能描述:通知已提交装修申请<br>
	 *
	 * @param requisitionId
	 * <br>
	 * Create author:罗军林<br>
	 * Create on:2008-12-31<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void notifySubmitRenovateRequisition(Long requisitionId) {
		RenovateRequisitionDO renovateRequisitionDO = renovateRequisitionDao.findById(requisitionId);
		renovateRequisitionDO.setState(SysrefcodeDO.RM_RENOVATE_REQUISITION_STATE_WAIT);
	}

	/**
	 * 功能描述:设置审批节点<br>
	 * 详细规则：1）当前节点审批全部通过 2）当前节点的所有审批结果将置为非当前节点，下一节点将置为当前节点
	 *
	 * @param renovateRequisitionId
	 * @param currentNote
	 * 当前节点
	 * @param nextNote
	 * 下一节点 Create author:罗军林<br>
	 * Create on:2009-1-2<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void setApprovalNote(List<RenovateApprovalDO> approvalList, String currentNote, String nextNote) {
		if (isApprovalPass(approvalList, currentNote)) {
			for (RenovateApprovalDO item : approvalList) {
				if (currentNote.equals(item.getLevel())) {
					item.setCurrentNodeYn(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO);
				} else if (nextNote != null && nextNote.equals(item.getLevel())) {
					item.setCurrentNodeYn(SysrefcodeDO.CONSTANTS_DIGITAL_ONE);
				}
			}
		}
	}


	public void setApprovalItemConfigDao(IApprovalItemConfigDao approvalItemConfigDao) {
		this.approvalItemConfigDao = approvalItemConfigDao;
	}

	public void setApprovalItemDao(IApprovalItemDao approvalItemDao) {
		this.approvalItemDao = approvalItemDao;
	}

	public void setDepositAccountApprovalDao(IDepositAccountApprovalDao depositAccountApprovalDao) {
		this.depositAccountApprovalDao = depositAccountApprovalDao;
	}

	public void setDepositAccountingDao(IDepositAccountingDao depositAccountingDao) {
		this.depositAccountingDao = depositAccountingDao;
	}


	public void setOrganizationalStructureDao(IOrganizationalStructureDao organizationalStructureDao) {
		this.organizationalStructureDao = organizationalStructureDao;
	}

	public void setRenovateApprovalDao(IRenovateApprovalDao renovateApprovalDao) {
		this.renovateApprovalDao = renovateApprovalDao;
	}

	public void setRenovateChargeService(IRenovateChargeService renovateChargeService) {
		this.renovateChargeService = renovateChargeService;
	}

	public void setRenovateRequisitionDao(IRenovateRequisitionDao renovateRequisitionDao) {
		this.renovateRequisitionDao = renovateRequisitionDao;
	}
}
